package controllers.rpcgame;

/**
 * @author Olga
 *
 */
public class RPCGameInstance {

	private String user1, user2;
	private int cardID1, cardID2;
	private int turnCounter;
	private String gameWinner;

	private int moveCounter;
	private int winner1Count, winner2Count;
	private char user1Turn, user2Turn;
	private String turnWinner;

	public RPCGameInstance(String user1, String user2, int cardID1, int cardID2) {
		// per game
		this.user1 = user1;
		this.user2 = user2;
		this.cardID1 = cardID1;
		this.cardID2 = cardID2;
		this.turnCounter = 0;

		// per turn
		this.moveCounter = 0;
		this.user1Turn = '\0';
		this.user2Turn = '\0';
		this.winner1Count = 0;
		this.winner2Count = 0;
		this.turnWinner = "";

	}

	public boolean turnFinished() {
		return (this.moveCounter == 2);
	}

	public void makeTurn(String username, String turn) {
		moveCounter++;
		if (username.equals(user1))
			user1Turn = turn.charAt(0);
		else
			user2Turn = turn.charAt(0);
	}

	public String getTurnWinner() {
		if (user1Turn == user2Turn) // tie
			this.turnWinner = "";
		// user 1 is winner
		else if ((user1Turn == 'r' && user2Turn == 's')
				|| (user1Turn == 's' && user2Turn == 'p')
				|| (user1Turn == 'p' && user2Turn == 'r')) {
			this.turnWinner = user1;
			this.winner1Count++;
		} else {
			// user2 is winner
			this.turnWinner = user2;
			this.winner2Count++;
		}
		return this.turnWinner;
	}

	public String getTurnLooser() {
		if (this.turnWinner.equals(""))
			return "";
		else if (this.turnWinner.equals(user1))
			return user2;
		else
			return user1;
	}

	public String getOpponentTurn(String player) {
		char oppTurn;

		if (player.equals(user1))
			oppTurn = user2Turn;
		else
			oppTurn = user1Turn;

		if (oppTurn == 'r')
			return "rock";
		if (oppTurn == 'p')
			return "paper";
		if (oppTurn == 's')
			return "scissors";
		return "";
	}

	public void initForNextTurn() {
		this.moveCounter = 0;
		this.turnWinner = "";
		this.user1Turn = '\0';
		this.user2Turn = '\0';
		this.turnCounter++;
	}

	public boolean isFinished() {
		return (turnCounter >= 3)
				|| (Math.abs(this.winner1Count - this.winner2Count) >= 2);
	}

	public String getWinner() {
		this.gameWinner = "";
		if (this.winner1Count > this.winner2Count)
			this.gameWinner = user1;
		else if (this.winner1Count < this.winner2Count)
			this.gameWinner = user2;
		return this.gameWinner;
	}

	public String getLooser() {
		if (this.gameWinner.equals(""))
			return "";
		if (this.gameWinner.equals(user1))
			return this.user2;
		return this.user1;
	}

	public int getPlayerCard(String player) {
		if (player.equals(user1))
			return this.cardID1;
		if (player.equals(user2))
			return this.cardID2;
		return 0;
	}

	public String getUser1() {
		return user1;
	}

	public String getUser2() {
		return user2;
	}

	public int getCardID1() {
		return cardID1;
	}

	public int getCardID2() {
		return cardID2;
	}
	
	

}
